package com.myplaylist.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.myplaylist.android.service.MyPlaylistService;

public class MyPlaylistActivity extends Activity {
	private Intent serviceIntent;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        serviceIntent = new Intent(this, MyPlaylistService.class);
        startService(serviceIntent);
        
        //repositoryManager = RepositoryManager.getInstance(getApplicationContext());
        
        /*wifiReceiver = new BroadcastReceiver() {
			
        	private Socket socket;

			@Override
        	public void onReceive(Context context, Intent intent) {
        		NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        		if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {

        			if (networkInfo.isConnected()) {
        				wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        				discoverer = new Discoverer(wifiManager);
        				synchronized (this) {
							networkInfoCredentials = discoverer.discover();
								try {
									sendTopTenToServer();
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
						}
        			} else {
            			synchronized (this) {
    						networkInfoCredentials = null;
    					}
            		}
        		} 
        	}

			private void sendTopTenToServer() throws Exception {
				List<Track> topTenTrackList = repositoryManager.getTopTen();
				if (topTenTrackList.size() != 0) {
					XmlSerializer xmlSerializer = Xml.newSerializer();
					StringWriter stringWriter = new StringWriter();

					xmlSerializer.setOutput(stringWriter);
					xmlSerializer.startDocument("UTF-8", true);
					xmlSerializer.startTag("", "tracklist");

					for (int i = 0; i < topTenTrackList.size(); i++) {
						xmlSerializer.startTag("", "track");
						xmlSerializer.attribute("", "title", topTenTrackList.get(i).getTitle());
						xmlSerializer.attribute("", "author", topTenTrackList.get(i).getAuthor());
						xmlSerializer.attribute("", "score", "" + topTenTrackList.get(i).getScore());
						xmlSerializer.endTag("", "track");
					}
					xmlSerializer.endTag("", "tracklist");
					xmlSerializer.endDocument();

					socket = new Socket(InetAddress.getByName(networkInfoCredentials.getServerIp()), Integer.parseInt(networkInfoCredentials.getServerPort()));

					PrintWriter pw = new PrintWriter(socket.getOutputStream());

					Log.i("XML String", stringWriter.toString());

					pw.write(stringWriter.toString());
					pw.flush();

					pw.close();

					if (socket != null) {
						socket.close();
					}
				}
			}
		};
		
		registerReceiver(wifiReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
		*/
    }

	@Override
	protected void onDestroy() {
		super.onDestroy();
		//unregisterReceiver(wifiReceiver);
		stopService(serviceIntent);
	}
    
    
}